#region File Description
//-----------------------------------------------------------------------------
// GameplayScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Magnet.Engine.Entities;
using Magnet.Engine;
#endregion

namespace Magnet
{
    /// <summary>
    /// This screen implements the actual game logic. It is just a
    /// placeholder to get the idea across: you'll probably want to
    /// put some more interesting gameplay in here!
    /// </summary>
    class GameplayScreen : GameScreen
    {
        #region Fields

        ContentManager content;
        SpriteFont gameFont;

        PlayerManager playerManager;

        public static Boolean changeLevel = false;

        private Texture2D mouseCursor;
        private Vector2 mousePosition = Vector2.Zero;

        private int level;

        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public GameplayScreen()
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);

            ArrowManager.Initialize(MagnetGame.Graphics);

            UserInterface.Initialize();

            level = 1;
        }

        /// <summary>
        /// Load graphics content for the game.
        /// </summary>
        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");

            gameFont = content.Load<SpriteFont>("SpriteFonts\\gamefont");

            mouseCursor = content.Load<Texture2D>("hook");

            Player player = new Player(new Vector2(30.0f, 30.0f), new Vector2(64.0f, 96.0f));

            player = new Player(new Vector2(700.0f, 450.0f), new Vector2(64.0f, 96.0f));

            playerManager = new PlayerManager(player);

            World.Initialize("Level1",player);
            Sound.ChangeMusic(Sound.MusicList.Music1);
            // Load Content

            ArrowManager.LoadContent(content);

            UserInterface.LoadContent(content);
        }

        /// <summary>
        /// Unload graphics content used by the game.
        /// </summary>
        public override void UnloadContent()
        {
            content.Unload();
        }


        #endregion

        #region Update and Draw

        /// <summary>
        /// Updates the state of the game. This method checks the GameScreen.IsActive
        /// property, so the game will stop updating when the pause menu is active,
        /// or if you tab away to a different application.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            if (IsActive)
            {
                float elapsedSeconds = (float)gameTime.ElapsedGameTime.Milliseconds / 1000f;

                if (changeLevel)
                {
                    changeLevel = false;
                    level++;

                    if (level == 2)
                    {
                        World.LoadLevel("Level2", playerManager.GetPlayer());
                        Sound.ChangeMusic(Sound.MusicList.Music2);
                    }
                    else if (level == 3)
                    {
                        World.LoadLevel("Level3", playerManager.GetPlayer());
                        Sound.ChangeMusic(Sound.MusicList.Music3);
                    }
                    else if (level == 4)
                    {
                        World.LoadLevel("Level4", playerManager.GetPlayer());
                        Sound.ChangeMusic(Sound.MusicList.Music1);
                    }
                    else if (level == 5)
                    {
                        World.LoadLevel("Level5", playerManager.GetPlayer());
                        Sound.ChangeMusic(Sound.MusicList.Music2);
                    }
                    else if (level == 6)
                    {
                        CreditsScreen.ExitGame(ScreenManager);
                    }
                }
                else
                {
                    if (playerManager.gameState == PlayerManager.GameState.BulletTime)
                        elapsedSeconds /= 5f;

                    World.Update(elapsedSeconds);
                    playerManager.Update(elapsedSeconds);
                    ArrowManager.Update(elapsedSeconds);
                    mousePosition = InputManager.MousePosition;
                }

                UserInterface.Update(elapsedSeconds);
            }
        }

        /// <summary>
        /// Lets the game respond to player input. Unlike the Update method,
        /// this will only be called when the gameplay screen is active.
        /// </summary>
        public override void HandleInput()
        {
            if (InputManager.IsKeyTriggered(InputManager.BACK))
            {
                // If they pressed pause, bring up the pause menu screen.
                ScreenManager.AddScreen(new PauseMenuScreen());
                Sound.PlayCue(Sound.MENU_BACK);
            }
        }

        /// <summary>
        /// Draws the gameplay screen.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            ScreenManager.GraphicsDevice.Clear(ClearOptions.Target,
                                               Color.Chocolate, 0, 0);

            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate,
                SaveStateMode.SaveState);

            World.Draw(spriteBatch);

            UserInterface.Draw(spriteBatch);

            spriteBatch.Draw(mouseCursor, mousePosition, Color.White);

            spriteBatch.End();

            // If the game is transitioning on or off, fade it out to black.
            if (TransitionPosition > 0)
                ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha);

        }
        #endregion
    }
}
